<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/9/3
 * Time: 15:51
 */

namespace App\Repositories;

use App\Http\Helper\BeanHelper;
use App\Models\TestingUserInfo;

class TestingUserInfoRepository extends Repository
{
    /**
     * 根据ID获取用户的基本信息
     * @param $uid
     * @return mixed
     */
    public function getOneByUid($uid) {
        return BeanHelper::convertStdClsToArr(TestingUserInfo::where('uid', $uid)->first());
    }

    /**
     * 保存用户的基本信息
     * @param $uid
     * @param $name
     * @param $phone
     * @param $wechatId
     * @return bool|mixed
     */
    public function saveInfo($uid, $name, $phone, $wechatId) {
        if ($userInfo = $this->getOneByUid($uid)) {
            if ($userInfo['name'] != $name || $userInfo['phone'] != $phone || $userInfo['wechat_id'] != $wechatId) {
                $userInfo['name'] = $name;
                $userInfo['phone'] = $phone;
                $userInfo['wechat_id'] = $wechatId;
                return $this->update($userInfo);
            }
            return true;
        } else {
            $userInfo = [
                'uid' => $uid,
                'name' => $name,
                'phone' => $phone,
                'wechat_id' => $wechatId
            ];
            return $this->store($userInfo);
        }
    }

    /**
     * 添加用户数据
     * @param $data
     * @return mixed
     */
    public function store($data) {
        return TestingUserInfo::firstOrCreate($data);
    }

    /**
     * 修改用户基本信息
     * @param $uid
     * @param $data
     * @return mixed
     */
    public function updateByUid($uid, $data) {
        $data['update_time'] = time();
        return TestingUserInfo::where('uid', $uid)->update($data);
    }

    /**
     * 修改用户基本信息
     * @param $data
     * @return mixed
     */
    public function update($data) {
        $data['update_time'] = time();
        return TestingUserInfo::where('id', $data['id'])->update($data);
    }

    public function model()
    {
        return app(TestingUserInfo::class);
    }

    public function tag()
    {
        // TODO: Implement tag() method.
    }
}